package test;
import nme.events.Event;
import nme.events.IOErrorEvent;
import nme.events.SecurityErrorEvent;
import nme.net.URLLoader;
import nme.net.URLLoaderDataFormat;
import nme.net.URLRequest;
import nme.net.URLRequestMethod;
import nme.utils.ByteArray;

/**
 * ...
 * @author liwei
 */

class TestUrlloader extends TestBase
{
	private var _loader:URLLoader;
	private var _url:String = "http://127.0.0.1/server/bin/index.php";
	public function new() 
	{
		super();
		_loader = new URLLoader();
		_loader.dataFormat = URLLoaderDataFormat.BINARY;
		_loader.addEventListener(Event.COMPLETE, completehandler);
		_loader.addEventListener(IOErrorEvent.IO_ERROR, iohandler);
		_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityhandler);
		log("TestStream");
	}
	
	private function securityhandler(e:SecurityErrorEvent):Void 
	{
		log("SecurityErrorEvent");
	}
	
	private function iohandler(e:IOErrorEvent):Void 
	{
		log("IOErrorEvent");
	}
	
	private function completehandler(e:Event):Void 
	{
		var ba:ByteArray = cast(_loader.data,ByteArray);
		log(ba.length+":ok");
	}
	override public function start():Void 
	{
		var ba:ByteArray = new ByteArray();
		ba.writeInt(100);
		ba.writeUTF("FF003d");
		ba.writeUTF("liwei");
		ba.writeUTF("123456");
		var request:URLRequest = new URLRequest();
		request.method = URLRequestMethod.POST;
		request.data = ba;
		request.contentType = "application/octet-stream";
		request.url = _url + "?login";
		_loader.load(request);
	}
	
}